package com.aaa.system.service.imp;

import com.aaa.entity.RegisteredItem;
import com.aaa.entity.User;
import com.aaa.system.mapper.RegisteredMapper;
import com.aaa.system.mapper.UserMapper;
import com.aaa.system.service.RegisteredService;
import com.aaa.system.vo.RegisteredItemVo;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service
public class RegisteredServiceImp implements RegisteredService {

    @Autowired
    private RegisteredMapper registeredMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result<IPage<RegisteredItem>> getAllData(RegisteredItemVo registeredItemVo) {
        IPage page = new Page(registeredItemVo.getCurrent(),registeredItemVo.getSize());
        QueryWrapper<RegisteredItem> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(registeredItemVo.getRegItemName())){
            wrapper.like("reg_item_name",registeredItemVo.getRegItemName());
        }
        if (Objects.nonNull(registeredItemVo.getStatus())){
            wrapper.eq("status",registeredItemVo.getStatus());
        }
        wrapper.eq("del_flag",0);
        IPage page1 = registeredMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    /**
     * 查询全部的数据 远程调用使用
     * @return
     */
    @Override
    public List<RegisteredItem> getList() {
        QueryWrapper<RegisteredItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("del_flag",0);
        return registeredMapper.selectList(wrapper);
    }

    @Override
    public Result<Boolean> saveOrUpdRegistered(RegisteredItem registeredItem) {
        String token = WebUtil.getRequest().getHeader("token");
        String phone = TokenUtils.getInfo(token).get("username").toString();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        String userName = userMapper.selectOne(wrapper).getUserName();

        if (Objects.nonNull(registeredItem.getRegItemId())){
            //修改
            registeredItem.setUpdateTime(LocalDateTime.now());
            registeredItem.setUpdateBy(userName);
            int i = registeredMapper.updateById(registeredItem);
            return new Result<>(200,"修改挂号费用设置成功",i>0?true:false);
        }
        registeredItem.setCreateTime(LocalDateTime.now());
        registeredItem.setCreateBy(userName);
        registeredItem.setDelFlag("0");
        int i = registeredMapper.insert(registeredItem);
        return new Result<>(200,"添加挂号费用设置成功",i>0?true:false);
    }

    @Override
    public Result<Boolean> delRegistered(Long[] regItemIds) {
        int i = registeredMapper.bachRegistered(regItemIds);
        return new Result<>(200,"删除挂号费用设置信息成功",i>0?true:false);
    }
}
